import { getComponentCompileDir } from "../utils/commonUtils";
import path from "path";
import fs from "fs";
import * as vscode from "vscode";

/**
* Opens or creates a customizedCommands.json configuration file in the .componentCompile directory
*/
export async function openOrCreateCustomizedCommandsFile() {
   const componentCompileDir = getComponentCompileDir();
   const customizedCommandsFilePath = path.join(componentCompileDir, 'customizedCommands.json');
   
   // Ensure .componentCompile directory exists
   if (!fs.existsSync(componentCompileDir)) {
       fs.mkdirSync(componentCompileDir, { recursive: true });
   }
   
   // Check if file exists
   if (!fs.existsSync(customizedCommandsFilePath)) {
       // Create file with default content
       const defaultContent = JSON.stringify({ 
           commands: [
               {
                   name: "Example Command",
                   command: "echo 'Hello World'",
                   description: "An example command that prints Hello World"
               },
               {
                   name: "Example Command 2",
                   command: "echo 'Hello World 2'",
                   description: "An example command that prints Hello World 2"
               }
           ]
       }, null, 2);
       try {
           fs.writeFileSync(customizedCommandsFilePath, defaultContent, 'utf8');
           vscode.window.showInformationMessage('Created customizedCommands.json configuration file in .componentCompile directory');
       } catch (error) {
           vscode.window.showErrorMessage(`Failed to create customized commands file: ${error instanceof Error ? error.message : String(error)}`);
           return;
       }
   }
   
   // Open the file in editor
   const document = await vscode.workspace.openTextDocument(customizedCommandsFilePath);
   await vscode.window.showTextDocument(document);
}
